/*****************************************************************
 * Author: DO HOANG HAI
 * Date of creation: 3rd/Jan/2008
 * Last modification: 25/Jan/2008
 * 
 * Description: Provide means to obtain and manage the Buddy data
 * from the file buddy.xml in the database
 * 
 * Status: Optimized and Finished
 ****************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Pbdms.DataAccess.DataProvider
{
    /// <summary>
    /// a buddy provider manage performing transactions to database (read/write)
    /// </summary>
    public class BuddyProvider
    {
        const string FILENAME = "buddy.xml";
        /// <summary>
        /// create a BuddyProvider object
        /// </summary>
        /// <returns></returns>
        public static BuddyProvider create()
        {
            return new BuddyProvider();
        }
        /// <summary>
        /// get the list of all buddies from the database
        /// </summary>
        /// <returns></returns>
        public ArrayList getListOfBuddies()
        {
            ArrayList arlResult = new ArrayList();
            ArrayList arlName = XmlHelper.create("buddy.xml").selectMultipleElementValues("/root/ipHolder/name");
            ArrayList arlIPAdd = XmlHelper.create("buddy.xml").selectMultipleElementValues("/root/ipHolder/ipAdd");
            ArrayList arlId = XmlHelper.create("buddy.xml").selectMultipleAttributeValues("/root/ipHolder", "id");
            int index=0;
            if (arlId != null && arlId.Count > 0)
            {
                foreach (object id in arlId)
                {
                    Common.DataObject.Buddy bdd = new Pbdms.Common.DataObject.Buddy(int.Parse(arlId[index].ToString()), arlName[index].ToString(), arlIPAdd[index].ToString());
                    arlResult.Add(bdd);
                    index++;
                }
            }
            return arlResult;
        }
        /// <summary>
        /// update a buddy to database
        /// </summary>
        /// <param name="bdd"></param>
        public void updateBuddyToDB(Common.DataObject.Buddy bdd)
        {

            XmlHelper.create(FILENAME).updateTypedElement("/root/ipHolder[@id='"+ bdd.Id +"']"+ "/name", bdd.Name);
            XmlHelper.create(FILENAME).updateTypedElement("/root/ipHolder[@id='" + bdd.Id + "']" + "/ipAdd", bdd.IpAdd);
        }
        /// <summary>
        /// append a new buddy to the database
        /// </summary>
        /// <param name="bdd"></param>
        public void appendBuddyToDB(Common.DataObject.Buddy bdd)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<ipHolder id='" + bdd.Id + "'>");
            sb.Append("<name>"+ bdd.Name+"</name>");
            sb.Append("<ipAdd>"+ bdd.IpAdd+"</ipAdd>");
            sb.Append("</ipHolder>");
            XmlHelper.create(FILENAME).appendChild("/root",sb.ToString());
        }
        /// <summary>
        /// delete a buddy from the database
        /// </summary>
        /// <param name="bdd"></param>
        public void deleteBuddyFromDB(Common.DataObject.Buddy bdd)
        {
            XmlHelper.create(FILENAME).deleteSelf("/root/ipHolder[@id='" + bdd.Id + "']");
        }
    }
}
